import {
    DemoList,
    DemoDetail
} from '../mutation-types';

import {
    Cache,
    cache_keys
} from '../../drivers/utils';

export const queryDemoList = function ({dispatch, state}, page=1) {

    //this.$http.get(this.$configs.baseUrl+ 'api/activity/myList', { parames: { page }}, {
    this.$http.get('./static/json/demo/list_'+ page + '.json').then(res => {

        this.$Loading.done();

        res = res.json();

        if (res.status) {
            var data = res.data;
            if(page > 1){
                data = state.demo.demoList.concat(data);
            }
            dispatch(DemoList, data);

            if(res.meta && res.meta.pagination){
                this.$emit('pageLoaded', true, res.meta.pagination);
            }else{
                this.$emit('pageLoaded', false);
            }
        }else{
            this.$Error( res.message || '加载错误');
        }

    },() => {
        this.$Loading.done();
        this.$Error('加载失败');
    })
};

export const queryDemoDetail = function ({dispatch, state}, id) {

    var oauth = Cache.get(cache_keys.token);

    this.$http
        .get('./static/json/demo/detail_'+ id + '.json', {
            header: {
                Authorization: oauth.access_token
            }
        })
        .then(res => {
            this.$Loading.done();
            res = res.json();

            if (res.status) {
                dispatch(DemoDetail, res.data)
            } else {
                this.$Error( res.message || '加载错误');
            }
        }, () => {
            this.$Loading.done();
            this.$Error('加载失败');
        })
};
